package com.teen9g.infoplatformserver.controller;

import com.teen9g.dto.UserLoginDTO;
import com.teen9g.entity.User;
import com.teen9g.infoplatformserver.service.UserService;
import com.teen9g.result.Result;
import com.teen9g.utils.JwtUtil;
import com.teen9g.vo.UserLoginVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author teen9g
 * 用户控制层
 */


@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    private int a = 0;

    @Autowired
    private UserService userService;

    /**
     * 页面刷新时调用的测试接口
     *
     * @return 测试结果
     */
    @GetMapping("/test")
    public String test(HttpServletRequest request) {
        a++;
        // 获取用户的IP地址
        String ip = request.getRemoteAddr();
        // 获取用户的User-Agent信息 包含设备信息
        String userAgent = request.getHeader("User-Agent");
        log.info("test：{}，IP：{}，User-Agent：{}", a, ip, userAgent);
        return "FUCK";
    }

    /**
     * 获取所有用户
     *
     * @return 所有用户
     */
    @GetMapping("/get")
    public List<User> getUser() {
        return userService.getUser();
    }

    /**
     * 用户登录
     *
     * @param userLoginDTO 用户名和密码
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("员工登录：{}", userLoginDTO);

        //查询用户
        User user = userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        if (user != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", user.getUsername());
            claims.put("id", user.getId());
            String token = JwtUtil.generateToken(user, claims);
            UserLoginVO userLoginVO = UserLoginVO.builder()
                    .id(user.getId())
                    .username(user.getUsername())
                    .token(token)
                    .build();
            log.info("登录成功：{}", userLoginVO);
            return Result.success(userLoginVO);
        }
        return Result.error("用户名或密码错误哦");

    }

    /**
     * 用户注册
     *
     * @param userloginDTO 用户名和密码
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserLoginDTO userloginDTO) {
        log.info("员工注册：{}", userloginDTO);
        String registerResult = userService.register(userloginDTO);
        return Result.success(registerResult);
    }

    /**
     * 根据id查询用户
     *
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public Result<User> getUser(@PathVariable Integer id) {
        log.info("根据id查询用户:{}", id);
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 根据id查询用户头像
     *
     * @param id 用户id
     * @return 用户头像
     */
    @GetMapping("/getAvatar/{id}")
    public Result<String> getAvatar(@PathVariable Integer id) {
        log.info("根据id查询用户头像:{}", id);
        String avatar = userService.getAvatar(id);
        return Result.success(avatar);
    }

    /**
     * 更新用户头像
     *
     * @param id     用户id
     * @param avatar 用户头像
     * @return 更新结果
     */
    @PutMapping("/updateAvatar/{id}")
    public Result<String> updateAvatar(@PathVariable Integer id,
                                       @RequestParam("avatar") String avatar) {
        log.info("更新id为{}的用户头像:{}", id, avatar);
        String result = userService.updateAvatar(id, avatar);
        return Result.success(result);
    }


}
